<?php

namespace App\Dao;

use App\Model\BillsModel;

class BillsDao extends BaseDao
{
    public static function bills_query($params = [])
    {
        $query = BillsModel::query();
        $query->when(checkIsHasField($params,'user_id'),function ($query)use ($params){
            $query->where('user_id',$params['user_id']);
        });


        return $query;
    }


    //更新用户余额
    public static function moneyUpdate($user_id = 0,$nums = 0,$title = '')
    {
        UsersDao::builder_user_query([
            'user_id'=>$user_id
        ])->increment('balance',$nums);
        //添加记录
        self::bills_query()->create([
            'user_id'=>$user_id,
            'nums'=>$nums,
            'title'=>$title,
            'balance'=>UsersDao::builder_user_query([
                'user_id'=>$user_id
            ])->value('balance')
        ]);
    }

    public static function pageinate(array $params)
    {
        return self::bills_query($params)->orderByDesc('created_at')->paginate();
    }
}
